$(function () {

    //此处开始获取detail页面中的元素 从上到下
    //大背景图
    let $contentbox = $("#contentbox");
    // 标题和概述
    let $heading = $("#heading");
    let $title = $("#title");
    // 大图标
    let $l_logo = $("#l-logo");
    // 小图标、名称、类型
    let $s_logo = $("#s-logo");
    let $name = $("#name");
    let $type = $("#type");
    // 价格
    let $price = $("#price");
    // 详情页标题
    let $f_title = $("#f-title");
    // 介绍图
    let $feature = $("#feature");

    let $addToCart = $("#addToCart");
    let info = null;

    //获取url中#后面的字符
    var id = location.hash.slice(1);

    console.log(id);
    $.ajax({
        url: "/php/getGoodInfo.php",
        data: {
            id
        },
        type: "get",
        dataType: "json",
        success(data) {
            info = data.data;
            // $img.attr("src", data.data.goods_small_logo);
            // $title.html(data.data.cat_id);
            // $price.html("￥" + data.data.goods_price);
            // $introduce.html(data.data.goods_introduce);

            // 此处开始渲染页面从上到下
            // 大背景图
            var bgImg = JSON.parse(info.productBackground).backgroundImageUrl;
            $contentbox.css("background", `url(${bgImg}) 40% 0 no-repeat`);
            // 标题和概述
            var heading = JSON.parse(info.heading);
            $title.text(heading.title);
            $heading.append(heading.summary);
            // 大图标
            $l_logo.attr("src", info.logoUrl);
            // 小图标、名称、类型
            $s_logo.attr("src", JSON.parse(info.family).iconUrl);
            $name.text(info.title);
            $type.text(info.description);
            // 价格
            var price = JSON.parse(info.products);
            try{
                price = price[0].price.price.raw;
            }catch(e){
                console.log(e);
                price = 0;
            }finally{
                console.log(price);
            }
            $price.text("￥"+price);
            // 详情页标题
            var featureSections = JSON.parse(info.featureSections);
            $f_title.text(featureSections[0].title);
            // 详情
            var featureArr = featureSections[0].features;
            var length = featureArr.length;
            console.log(featureArr);
            console.log(length);
            var str = "";
            featureArr.forEach(value => {
                str += `
                    <div class="panel panel-default">
                    <img src="${value.imageUrl}" alt="">
                    <div class="panel-heading">
                    <h3 class="panel-title">${value.name}</h3>
                    </div>
                    <div class="panel-body">
                    ${value.description}
                    </div>
                </div>
                `
            });
            $feature.html(str);

        }
    })

    // 本地存储购物车
    // $addToCart.on("click", function () {
    //     var arr = JSON.parse(localStorage.getItem("aaa")) || [];
    //     var isExistObj = arr.find(function (value) {
    //         return value.goods_id === info.goods_id
    //     })
    //     if (isExistObj) {
    //         isExistObj.num++;
    //     } else {
    //         info.num = 1;
    //         arr.push(info);
    //     }
    //     localStorage.setItem("aaa", JSON.stringify(arr))
    //     console.log("add");
    //     var isLeave = confirm("是否跳转至购物车？");
    //     if (isLeave) {
    //         location.href = "./cart.html";
    //     }
    // })

    // 数据库存购物车
    $addToCart.on("click",function(){
        var userId = $.cookie('id');
        console.log(userId);
        if(!userId){
            var jump = confirm("请先登录");
            if(!jump){
                return;
            }else{
                location.href = "./login.html";
            }
        }
        $.ajax({
            url: "/php/addtocart.php",
            data: {
                id,
                userId
            },
            type: "get",
            dataType: "json",
            success(data) {
                alert("加入购物车成功！")
            }
        });
    }) 



    //注销
    var $logoutBtn = $("#logout");
    function clearCookie() {
        var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
        if (keys) {
            for (var i = keys.length; i--;)
                document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
                // $.cookie(keys[i],null);
            }
    }
    // console.log($logoutBtn);
    $logoutBtn.on("click", function () {
        // console.log("aaa");
        clearCookie();
        location.href = "../index.html";
    })
});